package com.xunhang.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xunhang.dto.AdminDTO;
import com.xunhang.dto.Result;
import com.xunhang.entity.Admin;
import com.xunhang.entity.News;
import com.xunhang.entity.Type;
import com.xunhang.mapper.NewsMapper;
import com.xunhang.mapper.TypeMapper;
import com.xunhang.service.INewsService;
import com.xunhang.service.ITypeService;
import com.xunhang.utils.AdminHolder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TypeService extends ServiceImpl<TypeMapper, Type> implements ITypeService {
    public Result queryAllType(){
        List<Type> list = super.query().list();
        return Result.ok(list);
    }

    public Result AddType(Type type){
        AdminDTO admin = AdminHolder.getAdmin();
        if(admin==null){
            return Result.fail("权限不足");
        }
        boolean save = super.save(type);
        if(save){
            return Result.ok();
        }else {
            return Result.fail("保存失败");
        }
    }
    public Result DeleteType(Integer id){
        AdminDTO admin = AdminHolder.getAdmin();
        if(admin==null){
            return Result.fail("权限不足");
        }
        boolean remove = super.removeById(id);
        if(remove){
            return Result.ok();
        }else {
            return Result.fail("删除失败");
        }
    }
    public Result UpdateType(Type type){
        AdminDTO admin = AdminHolder.getAdmin();
        if(admin==null){
            return Result.fail("权限不足");
        }
        boolean update = super.updateById(type);
        if(update){
            return Result.ok();
        }else {
            return Result.fail("删除失败");
        }
    }
    public Result queryTypeById(Integer id){
        return Result.ok(getBaseMapper().selectById(id));
    }
}
